From 3db6c2ffa49bcb328a33e44c9cc2c5ebe6ea4f70 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 28 Jun 2014 16:54:16 -0700 Subject: [PATCH] Accept -j as a command-line parameter This parameter will be used to control the number of concurrent builds that cargo has executing. --- src/bin/cargo-build.rs | 7 +++++-- src/bin/cargo-git-checkout.rs | 4 +++- src/bin/cargo-test.rs | 10 +++++++--- src/cargo/ops/cargo_compile.rs | 7 ++++--- src/cargo/util/config.rs | 17 ++++++++++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/bin/cargo-build.rs b/src/bin/cargo-build.rs index f88cf5405..5a475ef13 100755 --- a/src/bin/cargo-build.rs +++ b/src/bin/cargo-build.rs @@ -21,11 +21,13 @@ use cargo::util::important_paths::find_project_manifest; #[deriving(PartialEq,Clone,Decodable,Encodable)] pub struct Options { manifest_path: Option, - update_remotes: bool + update_remotes: bool, + jobs: Option, } hammer_config!(Options "Build the current project", |c| { c.short("update_remotes", 'u') + .short("jobs", 'j') }) fn main() { @@ -46,8 +48,9 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { }; let update = options.update_remotes; + let jobs = options.jobs; - ops::compile(&root, update, "compile", shell).map(|_| None).map_err(|err| { + ops::compile(&root, update, "compile", shell, jobs).map(|_| None).map_err(|err| { CliError::from_boxed(err, 101) }) } diff --git a/src/bin/cargo-git-checkout.rs b/src/bin/cargo-git-checkout.rs index a7c190103..8ce3bee7a 100644 --- a/src/bin/cargo-git-checkout.rs +++ b/src/bin/cargo-git-checkout.rs @@ -37,7 +37,9 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { let source_id = SourceId::for_git(&url, reference.as_slice()); - let mut config = try!(Config::new(shell, true).map_err(|e| CliError::from_boxed(e, 1))); + let mut config = try!(Config::new(shell, true, None).map_err(|e| { + CliError::from_boxed(e, 1) + })); let mut source = GitSource::new(&source_id, &mut config); try!(source.update().map_err(|e| { diff --git a/src/bin/cargo-test.rs b/src/bin/cargo-test.rs index 5adb71120..4becf95a6 100755 --- a/src/bin/cargo-test.rs +++ b/src/bin/cargo-test.rs @@ -21,10 +21,13 @@ use cargo::util::important_paths::find_project_manifest; #[deriving(PartialEq,Clone,Decodable)] struct Options { manifest_path: Option, - rest: Vec + jobs: Option, + rest: Vec, } -hammer_config!(Options "Run the package's test suite") +hammer_config!(Options "Run the package's test suite", |c| { + c.short("jobs", 'j') +}) fn main() { execute_main_without_stdin(execute); @@ -41,7 +44,8 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { })) }; - try!(ops::compile(&root, false, "test", shell).map(|_| None::<()>).map_err(|err| { + try!(ops::compile(&root, false, "test", shell, options.jobs) + .map(|_| None::<()>).map_err(|err| { CliError::from_boxed(err, 101) })); diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index f8a1b9e23..789f65e3e 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -31,7 +31,8 @@ use sources::{PathSource}; use util::{CargoResult, Wrap, config, internal, human}; pub fn compile(manifest_path: &Path, update: bool, - env: &str, shell: &mut MultiShell) -> CargoResult<()> + env: &str, shell: &mut MultiShell, + jobs: Option) -> CargoResult<()> { log!(4, "compile; manifest-path={}", manifest_path.display()); @@ -51,7 +52,7 @@ pub fn compile(manifest_path: &Path, update: bool, let source_ids = package.get_source_ids(); let packages = { - let mut config = try!(Config::new(shell, update)); + let mut config = try!(Config::new(shell, update, jobs)); let mut registry = try!(PackageRegistry::new(source_ids, override_ids, &mut config)); @@ -70,7 +71,7 @@ pub fn compile(manifest_path: &Path, update: bool, target.get_profile().get_env() == env }).collect::>(); - let mut config = try!(Config::new(shell, update)); + let mut config = try!(Config::new(shell, update, jobs)); try!(ops::compile_targets(targets.as_slice(), &package, &PackageSet::new(packages.as_slice()), &mut config)); diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 898d025b5..34e7ec5a6 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -10,18 +10,25 @@ use cargo_toml = util::toml; pub struct Config<'a> { home_path: Path, update_remotes: bool, - shell: &'a mut MultiShell + shell: &'a mut MultiShell, + jobs: uint, } impl<'a> Config<'a> { - pub fn new<'a>(shell: &'a mut MultiShell, update_remotes: bool) -> CargoResult> { + pub fn new<'a>(shell: &'a mut MultiShell, + update_remotes: bool, + jobs: Option) -> CargoResult> { + if jobs == Some(0) { + return Err(human("jobs must be at least 1")) + } Ok(Config { home_path: try!(os::homedir().require(|| { human("Cargo couldn't find your home directory. \ This probably means that $HOME was not set.") })), update_remotes: update_remotes, - shell: shell + shell: shell, + jobs: jobs.unwrap_or(os::num_cpus()), }) } @@ -40,6 +47,10 @@ impl<'a> Config<'a> { pub fn update_remotes(&mut self) -> bool { self.update_remotes } + + pub fn jobs(&mut self) -> uint { + self.jobs + } } #[deriving(Eq,PartialEq,Clone,Encodable,Decodable)] -- 2.30.2